package zxj.spay.modular.report.ctrl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import zxj.spay.anotation.NoAccess;
import zxj.spay.base.Context;
import zxj.spay.entity.Type;
import zxj.spay.entity.User;
import zxj.spay.modular.data.service.DataService;
import zxj.spay.modular.report.bean.IndexTotal;
import zxj.spay.modular.report.service.ReportService;
/**
 * 数据统计
 * @author 朱小杰
 * 日期:2016-05-09
 */
@Controller
@RequestMapping("/report")
public class ReportCtrl extends Context{
	public final static Logger LOG = Logger.getLogger(ReportCtrl.class);
	@Resource
	private ReportService reportService;
	@Resource
	private DataService dataService;
	
	/**
	 * 项目主页
	 * @return
	 */
	@RequestMapping("/home.htm")
	public String home(Map<String,Object> data,HttpServletRequest request){
		User user = getUser(request);
		//获取首页的四组统计数据（日，周，月，年）
		Map<String,IndexTotal> totals = reportService.getIndexListTotal(user.getId());
		//获取 本月支出分布图 		饼图
		List<Map<String,Object>> pice = reportService.getThisMonthCostAreaTotal(user.getId());
		//获取支出每天的统计
		List<Map<String,Object>> moth_day_cost = reportService.getThisMonthCostDayTotal(user.getId());
		//获取收入每天的统计
		List<Map<String,Object>> moth_day_income = reportService.getThisMonthIncomeDayTotal(user.getId());
		//获取每天
		int day_of_month = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
		List<Integer> days = new ArrayList<Integer>();
		for(int i = 1 ; i <= day_of_month ; i ++){
			days.add(i);
		}
		data.put("days",days);
		data.put("totals", totals);
		data.put("pice",pice);
		data.put("month_line_cost",moth_day_cost);
		data.put("month_line_income",moth_day_income);
		return "/WEB-INF/view/home.ftl";
	}
	
	/**
	 * 查看已创建的类型
	 * @param request
	 * @return
	 */
	@RequestMapping("/showType.htm")
	public String showType(HttpServletRequest request,Map<String,Object> data){
		int user_id = getUser(request).getId();
		//支出项
		List<Type> type_cost = dataService.getTypeByStatus(Type.STATUS_COST,user_id);
		//收入
		List<Type> type_income = dataService.getTypeByStatus(Type.STATUS_INCOME,user_id);
		data.put("type_cost", type_cost);
		data.put("type_income", type_income);
		return "/WEB-INF/view/showType.ftl";
	}
	
	/**
	 * 流水列表
	 * @return
	 */
	@NoAccess
	@RequestMapping("/stream.htm")
	public String stream(){
		return "/WEB-INF/view/streamList.ftl";
	}
}
